package pagehelper

import "gorm.io/gorm"

func Paginate(current int, size int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		current, size = verifyParams(current, size)
		offset := (current - 1) * size
		return db.Offset(offset).Limit(size)
	}
}

func verifyParams(page, pageSize int) (int, int) {
	if page == 0 {
		page = 1
	}
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	return page, pageSize
}
